var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    GroupBuyProduct:[],
    CurrentPage: 1,
    loadingMoreHidden:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGroupBuyList()
  },

  /**
   * 获取拼团商品列表
   */
  getGroupBuyList:function(){
    var that = this
    wx.request({
      url: app.globalData.host + '/api/Store/GetGroupBuyProduct?storeId=' + app.globalData.storeId + '&pageSize=' + 8 + '&pageNumber=' + that.data.CurrentPage,
      success:function(res){
        if(res.data.Code == 0){
          var pruduct = []
          var ProductList = res.data.Data.Items
          var rawData = res.data.Data.Page;
          for (var i = 0; i < ProductList.length;i++){
            if (ProductList[i].Product){
              var c = {}
              c.Pid = ProductList[i].Product.Pid
              c.Name = ProductList[i].Product.Name
              c.ShowImg = app.globalData.image800_800host + ProductList[i].Product.ShowImg
              c.SaleCount = ProductList[i].Product.SaleCount
              c.ShopPrice = ProductList[i].Product.ShopPrice
              pruduct.push(c)
            }
          }
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.GroupBuyProduct
            pruduct = preGoods.concat(pruduct);
          }
          that.setData({
            GroupBuyProduct: pruduct,
            loadingMoreHidden: rawData.HasNextPage
          })
        }
      }
    })
  },

  /**
   * 加载更多
   */
  loadMore: function (e) {
    var that = this;
    if (!that.data.loadingMoreHidden) return;
    var currPage = that.data.CurrentPage;
    that.setData({
      CurrentPage: currPage + 1
    })
    that.getGroupBuyList();
  },

  /**
   * 跳转详情页
   */
  toDetailsGroup:function(e){
    var pid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/pintuan-details/index?id=' + pid,
    })
  }
})